//蒙板
let mask = new Mask('images/qrcode2.png','images/qrcode3.png','images/qrcode1.png')
mask.init();
//消息
let massage1 = new Announcement('系统公告','老段又胖十斤<br>老段又胖十斤<br>老段又胖十斤<br>','2022',100)
massage1.warning()
let massage2 = new Announcement('系统公告','老段又胖十斤<br>老段又胖十斤<br>老段又胖十斤<br>','2021',200)
massage2.success()
let massage3 = new Announcement('系统公告','老段又胖十斤<br>老段又胖十斤<br>老段又胖十斤<br>','2020',300)
massage3.info()

const mainIframeContainer = document.querySelector("#mainIframeContainer");
const  dropdownContent  = document.querySelector(".dropdown-content");
//个人设置的广播
const bcc = new BroadcastChannel(20000);
//菜单和面包屑的广播
const bc = new BroadcastChannel(10000);

//接受传递过来href
bc.addEventListener("message",e=>{
    //从router对象中获取 路由
    if(!router[e.data]) {
        console.error(`${e.data} 路由不存在`);
        return;
    }
    //获取路径
    const path = router[e.data] ?? router["#/main"];
    //替换src
    mainIframeContainer.lastElementChild.setAttribute("src",path);
})

//个人设置的广播
bcc.addEventListener("message", (e) => {
    if(e.data){
        dropdownContent.style.display = "block";
    }else{
        dropdownContent.style.display = "none";
    }
});


function createA(text, href) {
    const a = document.createElement('a');
    a.textContent = text;
// 点击事件
    if(href){//不为空
        a.href = href;
        a.addEventListener("click", (e) => {
            bc.postMessage(href);
            dropdownContent.style.display = "none";
            bcc.postMessage(true);
        },true);
        return a;

    }

    //为空别急   先设置单机事件 在设置双击事件
    let restoreTimer = null;
    a.onclick = (e) => {
        if(a.textContent == "退出登录"){
            const originalText = a.textContent;
            a.textContent = "确定退出登录么？";
            a.classList.add("logout-btn","shake")
            if (restoreTimer) clearTimeout(restoreTimer);//清理定时器
            restoreTimer = setTimeout(() => {
                a.textContent = originalText;
                a.classList.remove("logout-btn","shake");
            }, 3000);
            a.addEventListener("dblclick", e=>{
                window.location.href = "Login/login.html";
            })
        }
    }
    return a;
}

const linksData = [
    { text: '个人中心',href:'#/Personalcenter/personalcenter'},
    { text: '账号设置' ,href:'#/Accountsetting/accountsetting'},
    { text: '切换账号'},
    { text: '退出登录'}
];


linksData.forEach(key => {
    const link = createA(key.text,key.href);
    dropdownContent.appendChild(link);
});
